package com.zhou.controller;

import com.alibaba.fastjson.JSON;
import com.zhou.bean.*;
import com.zhou.mvc.ResponseBody;
import com.zhou.service.FastManService;
import com.zhou.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserController {
    @ResponseBody("/user/console.do")
    public String userTotal(HttpServletRequest req, HttpServletResponse resp){
        List list = UserService.findAllAndAllDay();
        List list1 = FastManService.findAllAndAllDay();
        Integer userTotal = (Integer) list.get(0);
        Integer userDay = (Integer) list.get(1);
        Integer fastManTotal = (Integer) list1.get(0);
        Integer fastManDay = (Integer) list1.get(1);
        ArrayList<Map<String,Integer>> mapArrayList = new ArrayList<>();
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("user_total",userTotal);
        map1.put("user_day",userDay);
        HashMap<String, Integer> map2 = new HashMap<>();
        map2.put("fastMan_total",fastManTotal);
        map2.put("fastMan_day",fastManDay);
        mapArrayList.add(map1);
        mapArrayList.add(map2);
        Message message = new Message();
        message.setData(mapArrayList);
        return JSON.toJSONString(message);
    }
    @ResponseBody("/user/list.do")
    public String selectAll(HttpServletRequest req, HttpServletResponse resp){
        String pageIndex = req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        List<User> userList = UserService.selectAll(Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        int total = UserService.total();
        ResultData<User> data = new ResultData<>();
        data.setRows(userList);
        data.setTotal(total);
        return JSON.toJSONString(data);
    }
    @ResponseBody("/user/insert.do")
    public String insert (HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        String idCard = req.getParameter("idCard");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setUserPhone(userPhone);
        user.setIdCard(idCard);
        user.setPassword(password);
        int i = UserService.addUser(user);
//        System.out.println(i);
        Message message = new Message();
        if (i!=0){
            message.setStatus(0);
            message.setResult("增加成功");
        }else {
            message.setStatus(-1);
            message.setResult("增加失败");
        }
        System.out.println(message);
        return JSON.toJSONString(message);
    }
    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String userphone = req.getParameter("userphone");
        User user = UserService.findByUserPhone(userphone);
        //System.out.println(express);
        Message msg = new Message();
        if (user.getUserPhone()==null){
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(user);
        }
        return JSON.toJSONString(msg);
    }
    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String userphone = req.getParameter("userphone");
        int i = UserService.deleteByUserPhone(userphone);
        Message message = new Message();
        if (i!=0){
            message.setStatus(0);
            message.setResult("删除成功");
        }else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JSON.toJSONString(message);
    }
    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        String idCard = req.getParameter("idCard");
        String password = req.getParameter("password");
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setUsername(username);
        user.setUserPhone(userPhone);
        user.setIdCard(idCard);
        user.setPassword(password);
        int i = UserService.updateUser(Integer.parseInt(id), user);
        Message message = new Message();
        if (i != 0){
            message.setStatus(0);
            message.setResult("修改成功");
        }else {
            message.setStatus(-1);
            message.setResult("修改失败");
        }
        return JSON.toJSONString(message);
    }
    @ResponseBody("/user/count.do")
    public String userCount(HttpServletRequest req, HttpServletResponse resp){
        List<UserConut> userConutList = UserService.selectUserCount();
        Message message = new Message();
        message.setStatus(0);
        message.setData(userConutList);
        return  JSON.toJSONString(message);
    }
}
